<?php
function Mres($string)
{
	//include "connections/dbconnect.php";
	
	if(get_magic_quotes_gpc())
	{
		$string = stripslashes($string);
	}
	
	return mysql_real_escape_string(trim($string));	 
}

function LoggedIn() //IF LOGGED IN RETURN TRUE
{
	if(isset($_SESSION["userID"])||(isset($_COOKIE["ac"])))
	{
		return true;
	}
}

function Fname($userID) //RETURN FIRST NAME
{
	$userID = Mres($userID);
	
	$queryFirstname = mysql_query("SELECT fname FROM Users WHERE ID = '{$userID}'");
	$row = mysql_fetch_assoc($queryFirstname);
	return $row["fname"];
}

function Lname($userID) //RETURN LAST NAME
{
	$userID = Mres($userID);
	
	$queryLastname = mysql_query("SELECT lname FROM Users WHERE ID = '{$userID}'");
	$row = mysql_fetch_assoc($queryLastname);
	return $row["lname"];
}

function DisplayName($userID) //RETURN DISPLAY NAME
{
		return Fname($userID)." ".Lname($userID);
}

function Username($userID)
{
	$userID = Mres($userID);
	$queryUsername = mysql_query("SELECT username FROM Users WHERE ID = '{$userID}'");
	$row = mysql_fetch_assoc($queryUsername);
	return $row["username"];
}	

function UsernameToUid($username)
{
	$username = Mres($username);
	
	$queryUid = mysql_query("SELECT ID FROM Users WHERE username = '{$username}'");
	
	if(mysql_num_rows($queryUid) > 0)
	{
		$row = mysql_fetch_assoc($queryUid);
		return $row["ID"];
	}
	else
	{
		return false;
	}
}

function UserExist($userID)
{
	$userID = Mres($userID);

	if(mysql_num_rows(mysql_query("SELECT ID FROM Users WHERE ID = '{$userID}'")) > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function ToProfile($userID) //RETURN LINK TO PROFILE
{
		return '/profile/@'.Username($userID);
}

function IsMe($userID)
{
	if($userID == $_SESSION["userID"])
	{
		return true;
	}
	else
	{
		return false;
	}
}

function IsFollowing($uid1, $uid2)
{
	$followerID = Mres($uid1);
	$followingID = Mres($uid2);
	
	$queryIsFollowing = mysql_query("
		SELECT COUNT(*) AS 'isFollowing'
		FROM squiblo_socialnews.Followings
		WHERE (followerID = '{$followerID}' AND followingID = '{$followingID}')
		OR (followerID = '{$followingID}' AND followingID = '{$followerID}' AND mutual = '1')
	");
	
	$row = mysql_fetch_assoc($queryIsFollowing);
	
	if($row["isFollowing"] == 1){
		return true;
	} else {
		return false;
	}
}

function CheckPort($address, $port) {

	$checkport = @fsockopen($address, $port, $errnum, $errstr, 2); //The 2 is the time of ping in secs

	// If port is closed
	if(!$checkport){
		return false;
	}else{
		return true;	
	}
}

function sendMessage($to, $message, $attachment, $from){
	
	
}

function time_stamp($session_time) 
{ 
	$time_difference = time() - $session_time; 

	$seconds = $time_difference; 
	$minutes = round($time_difference / 60 );
	$hours = round($time_difference / 3600 ); 
	$days = round($time_difference / 86400 ); 
	$weeks = round($time_difference / 604800 ); 
	$months = round($time_difference / 2419200 ); 
	$years = round($time_difference / 29030400 );

	// Seconds
	if($seconds == 0)
	{
		return "a few seconds ago";	
	}
	else if($seconds <= 60 && $seconds > 0)
	{
		return $seconds." seconds ago"; 
	}
	//Minutes
	else if($minutes <= 59)
	{
		if($minutes == 1)
	    {
			return "a minute ago"; 
		}
	    else
	    {
			return $minutes." minutes ago"; 
	    }
	}
	//Hours
	else if($hours <= 24)
	{
		if($hours == 1)
		{
			return "an hour ago";
		}
		else
		{
			return $hours." hours ago";
		}
	}
	//Days
	else if($days <= 6)
	{
		if($days == 1)
		{
			return "a day ago";
		}
		else
		{
			return $days." days ago";
		}
	}
	//Weeks
	else if($weeks <= 4)
	{
		if($weeks == 1)
		{
			return date('l \a\t g:ia ', $session_time);
		}
		else
		{
			$that = date('F', $session_time + 60 * 60);
			$today = date('F', mktime() + 60 * 60);
	   
			if($that == $today)
			{
				return date('\T\h\e jS \a\t g:ia ', $session_time);
			}
			else
			{
				return date('F jS \a\t g:ia ', $session_time);  
			}
		}
	}
	//Months
	else if($months <= 12)
	{
		if($months == 1)
		{
			return date('F jS \a\t g:ia ', $session_time);
		}
		else
		{
			return date('F jS \a\t g:ia ', $session_time);
		}
	}
	//Years
	else
	{
		return date('F jS, Y \a\t g:ia ', $session_time);
	}
}

function MutualFollowers($userID){
	$userID = Mres($userID);
	
	$mutualFollowers = array();
	
	$mutualFollowersQuery = mysql_query("SELECT followingID FROM Followings WHERE followerID = '{$userID}' AND mutual='1'") or die(mysql_error());
	$mutualFollowersQuery2 = mysql_query("SELECT followerID FROM Followings WHERE followingID = '{$userID}' AND mutual='1'") or die(mysql_error());
	
	$i = 0;
	while($row = mysql_fetch_assoc($mutualFollowersQuery)){
		$mutualFollowers[$i] = $row['followingID'];
		$i++;
	}

	while($row = mysql_fetch_assoc($mutualFollowersQuery2)){
		$mutualFollowers[$i] = $row['followerID'];
		$i++;
	}
	
	return $mutualFollowers;
	
}
?>